package de.szut.SudokuGUI;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import de.szut.SudokuController.Controller;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;

import com.jgoodies.forms.factories.Borders;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

@SuppressWarnings("serial")
public class MainField extends JFrame implements IGUI {

	private JPanel contentPane;
	private JLabel activeLabel;
	private JLabel[][] labelList = new JLabel[9][9];
	private Controller controller = Controller.getInstance();
	private String fileName = controller.getFilename();
	private JButton tip;
		
	public JLabel getActiveLabel() {
		return activeLabel;
	}

	public MainField() {
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 425);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setTitle(fileName + " - Sudoku");
		setResizable(false);

		int additionX = 0;
		int additionY = 0;
		for (int i = 0; i < 9; i++) {
			additionX = 0;
			if (i % 3 == 0 && i != 0) {
				additionY = additionY + 5;
			}
			for (int j = 0; j < 9; j++) {
				final JLabel panel = new JLabel();
				panel.setOpaque(true);
				panel.setFont(new Font("Tahoma", Font.PLAIN, 19));
				panel.setBackground(Color.ORANGE);
				if (j % 3 == 0 && j != 0) {
					additionX = additionX + 5;
				}
				panel.setBounds(10 + (i * 40) + additionY, 10 + (j * 40) + additionX, 35, 35);
				contentPane.add(panel);
				labelList[j][i] = panel;
				panel.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent arg0) {
						try {
							activeLabel.setBackground(Color.ORANGE);
						} catch(Exception e) {
						}
						activeLabel = panel;
						activeLabel.setBackground(Color.RED);
					}
				});				
			}
		}
		int counterInt = 1;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				final JButton button = new JButton(Integer.toString(counterInt));
				button.setFont(new Font("Tahoma", Font.PLAIN, 18));
				button.setBounds(425 + (i * 47), 125 + (j * 47), 46, 46);
				contentPane.add(button);
				if (counterInt <= 6)
					counterInt = counterInt + 3;
				else {
					counterInt = counterInt - 5;
				}
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							activeLabel.setBackground(Color.ORANGE);
							controller.clickedNumber(activeLabel, Integer.parseInt(button.getText()));
							activeLabel = null;
						} catch(Exception e) {
							
						}
					}
				});
			}
		}
		final JButton clear = new JButton("Feld leeren");
		clear.setFont(new Font("Tahoma", Font.PLAIN, 18));
		clear.setBounds(426, 267, 138, 37);
		contentPane.add(clear);
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					controller.clickedEmptyer(activeLabel);
					activeLabel.setBackground(Color.ORANGE);
					activeLabel = null;
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
		});
								
		JButton save = new JButton("Speichern");
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					controller.clickedSave();
				} catch (IOException e) {
					e.printStackTrace();
				}
				fileName = controller.getFilename();
				setTitle(fileName + " - Sudoku");
			}
		});
		save.setFont(new Font("Tahoma", Font.PLAIN, 18));
		save.setBounds(679, 100, 138, 37);
		contentPane.add(save);

		JButton load = new JButton("Laden");
		load.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					controller.clickedLoad();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				fileName = controller.getFilename();
				setTitle(fileName + " - Sudoku");
			}
		});
		load.setFont(new Font("Tahoma", Font.PLAIN, 18));
		load.setBounds(679, 148, 138, 37);
		contentPane.add(load);

		tip = new JButton("Hinweis: " + controller.getTip());
		tip.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.clickedTip();
			}
		});
		tip.setFont(new Font("Tahoma", Font.PLAIN, 18));
		tip.setBounds(679, 196, 138, 37);
		contentPane.add(tip);
		
		JButton solve = new JButton("Lösen");
		solve.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.clickedSolve();
			}
		});
		solve.setFont(new Font("Tahoma", Font.PLAIN, 18));
		solve.setBounds(679, 244, 138, 37);
		contentPane.add(solve);
		
		JButton button = new JButton("Fertig!");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.finish();
			}
		});
		button.setFont(new Font("Tahoma", Font.PLAIN, 18));
		button.setBounds(679, 292, 138, 37);
		contentPane.add(button);
		contentPane.revalidate();
		contentPane.repaint();
	}

	@Override
	public void refresh(int[][] field) {
		//Hinweis Button wird refresht
		tip.setText("Hinweise: " + controller.getTip());														//udhfguiehf
		//Label werden erneuert
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				if (field[i][j] > 0) {
					labelList[i][j].setText("  " + Integer.toString(field[i][j]));
				}
				else {
					labelList[i][j].setText(" ");
				}
			}
		}
		contentPane.revalidate();
		contentPane.repaint();
	}

	@Override
	public void show(String[][] field) {
		
	}

	@Override
	public void finish() {
		//Label werden grün gemacht
		for (JLabel[] j : labelList) {
			for (JLabel k : j) {
				k.setBackground(Color.GREEN);
			}
		}
		//Siegnachricht
		JOptionPane.showMessageDialog(null,
                "Fantastisch! Sie haben das Sudoku gelöst!",
                "Sudoku",					      
                JOptionPane.INFORMATION_MESSAGE);
	}

	@Override
	public int[] getPosition(JLabel label) {
		//2 For Schleifen, die durch die labellist iterieren
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				//Wenn das aktuelle label der Labellist gleich mit dem übergebenen ist, dann wird koordinaten returne
				if (labelList[j][i].equals(label)) {
					int[] temp = {j,i};
					return temp;
				}
			}
		}
		return null;
	}

	@Override
	public void getEingabe() {
	}
	
	
	
}
